package school.community.controller;

import school.community.dto.PaginationDTO;
import school.community.model.User;
import school.community.service.InfoService;
import school.community.service.NotificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@Slf4j
public class ProfileController {
    @Autowired
    private InfoService infoService;
    @Autowired
    private NotificationService notificationService;

    @GetMapping("/profile/{action}")
    public String profile(HttpServletRequest request,
                          @PathVariable(name = "action") String action,
                          @RequestParam(name = "page", defaultValue = "1") Integer page,
                          @RequestParam(name = "size", defaultValue = "5") Integer size,
                          Model model) {

        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return "redirect:/";
        }

        if ("questions".equals(action)) {
            model.addAttribute("section", "questions");
            model.addAttribute("sectionName", "我的说说");
            PaginationDTO paginationDTO = infoService.list(user.getId(), page, size);
            model.addAttribute("pagination", paginationDTO);
        } else if ("replies".equals(action)) {
            PaginationDTO paginationDTO = notificationService.list(user.getId(), page, size);
            Long unreadCount = notificationService.unreadCount(user.getId());
            //修改session中的未读数目
            request.getSession().setAttribute("unreadCount", unreadCount);
            model.addAttribute("section", "replies");
            model.addAttribute("pagination", paginationDTO);
            model.addAttribute("sectionName", "最新回复");
        } else if ("waitCheck".equals(action)){
            model.addAttribute("section", "waitCheck");
            model.addAttribute("sectionName", "等待审核");
            //根据活动公告的state来判断是否被审核
            PaginationDTO paginationDTO = infoService.listByState(page, size);
            model.addAttribute("pagination", paginationDTO);
        } else if("notice".equals(action)) {
            HttpSession session = request.getSession();
            User admin = (User)session.getAttribute("admin");
            Long adminId = admin.getId();
            //notice:公告
            PaginationDTO paginationDTO = infoService.list(adminId, page, size);
            model.addAttribute("section", "notice");
            model.addAttribute("pagination", paginationDTO);
            model.addAttribute("sectionName", "已发公告");
        }
        return "profile";
    }
}
